<template>
  <div>
    <el-dialog :title="form.id?'编辑分类':'添加分类'" :visible="showDialog" width="50%" @close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="50px">
        <el-form-item prop="title" label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item prop="desc" label="描述">
          <el-input v-model="form.desc" type="textarea" :rows="4" resize="none" />
        </el-form-item>
        <el-form-item label="封面">
          <UpImg ref="UpImg" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submit">确 定</el-button>
        <el-button size="mini" @click="close">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import UpImg from '@/components/UpImg/index.vue'
import { addQuantity, editQuantity } from '@/api/categoties'
export default {
  components: { UpImg },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        title: '',
        desc: '',
        image: ''
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.form = {
        title: '',
        desc: '',
        image: ''
      }
      this.$refs.UpImg.imgUrl = ''
      this.$refs.UpImg.handleRemove()
      this.$refs.form.resetFields()
    },
    async submit() {
      await this.$refs.form.validate()
      this.form.image = this.$refs.UpImg.dialogImageUrl || this.$refs.UpImg.imgUrl
      if (this.form.id) {
        await editQuantity(this.form)
      } else {
        await addQuantity(this.form)
      }
      await this.close()
      this.$emit('xrym')
    },
    // 回显图片
    EchoImg() {
      this.$refs.UpImg.dialogImageUrl = this.form.image
      this.$refs.UpImg.sss = 1
    }
  }
}
</script>

<style>
</style>
